package cn.aran.org.service.impl;

import cn.aran.basic.service.impl.BaseServiceImpl;
import cn.aran.org.domain.Department;
import cn.aran.org.mapper.DepartmentMapper;
import cn.aran.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @Description: 实现类
* @Author: Aran
* @Version: 1.0
* @Date:  2022/6/21 18:41
*/
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService{
    @Autowired
    private DepartmentMapper departmentMapper;

    /**
    * @Title: save
    * @Description: 部门新增
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/6/21 18:44
    * @Parameters: [department]
    * @Return void
    */
    @Transactional
    @Override
    public void save(Department department) {
        departmentMapper.save(department);
        // this.update(department);
        String dirPath = "";
        if (department.getParent().getId() == null){
            dirPath = "/" + department.getId();
        }else {
            Department parent = departmentMapper.loadById(department.getParent().getId());
            if(parent != null){
                dirPath = parent.getDirPath() + "/" + department.getId();
            }
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

    /**
    * @Title: update
    * @Description: 部门修改
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/6/21 18:44
    * @Parameters: [department]
    * @Return void
    */
    @Transactional
    @Override
    public void update(Department department) {
        String dirPath = "";
        if (department.getParent().getId() == null){
            dirPath = "/" + department.getId();
        }else {
            Department parent = departmentMapper.loadById(department.getParent().getId());
            if(parent != null){
                dirPath = parent.getDirPath() + "/" + department.getId();
            }
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

    /**
    * @Title: loadDeptTree
    * @Description: 部门无限级树
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/6/26 10:24
    * @Parameters: []
    * @Return java.util.List<cn.aran.org.domain.Department>
    */
    @Override
    public List<Department> loadDeptTree() {
        // 查询所有部门数据
        List<Department> allDept = departmentMapper.loadAll();
        Map<Long, Department> allMap = allDept.stream().collect(Collectors.toMap(Department::getId, item -> item));
        List<Department> departments = new ArrayList<>();
        for (Department deptEmp : allDept) {
            if (deptEmp.getParentId() == null) {
                departments.add(deptEmp);
            } else {
                Department department = allMap.get(deptEmp.getParentId());
                if (department != null) {
                    department.getChildren().add(deptEmp);
                }
            }
        }

        return departments;
    }

    // @Override
    // public List<Department> loadDeptTree() {
    //     // 查询所有部门数据
    //     List<Department> allDept = departmentMapper.loadAll();
    //     List<Department> departments = new ArrayList<>();
    //     // 循环判断部门级树
    //     for (Department deptEmp : allDept) {
    //         if (deptEmp.getParent_id() == null ){
    //             departments.add(deptEmp);
    //
    //         }else {
    //             for(Department children : allDept){
    //                 if (children.getId() == deptEmp.getParent_id()){
    //                     children.getChildren().add(deptEmp);
    //                     break;
    //                 }
    //             }
    //         }
    //     }
    //     return departments;
    // }
}
